package xcmg.device.service.warehouse;


import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xcmg.device.dao.entity.InventoryCheckDO;
import xcmg.device.dao.entity.InventoryCheckDetailDO;
import xcmg.device.dao.entity.InventoryCheckReportDO;
import xcmg.device.dao.entity.MaterialWarehouseUserDO;
import xcmg.device.dao.entity.PriMaterialsDO;
import xcmg.device.dao.entity.warehouse.WarehouseDO;
import xcmg.device.dao.mapper.InventoryCheckDetailMapper;
import xcmg.device.dao.mapper.InventoryCheckMapper;
import xcmg.device.dao.mapper.MaterialWarehouseUserMapper;
import xcmg.device.dao.mapper.PriMaterialsMapper;
import xcmg.device.dao.mapper.warehouse.WarehouseAreaLocationDOMapper;
import xcmg.device.dao.mapper.warehouse.WarehouseDOMapper;
import xcmg.device.dao.mapper.warehouse.WarehouseInventoryMapper;
import xcmg.device.infra.DateUtil;
import xcmg.device.infra.ErrorCode;
import xcmg.device.infra.ExportUtil;
import xcmg.device.infra.enums.BillDesc;
import xcmg.device.service.GenDocumentService;
import xcmg.device.service.vo.basic.MaterialsRelationVO;
import xcmg.device.service.vo.permission.WareHousePermission;
import xcmg.device.service.vo.warehouse.InventoryCheckCountVO;
import xcmg.device.service.vo.warehouse.InventoryCheckDetailVO;
import xcmg.device.service.vo.warehouse.InventoryCheckPrintVO;
import xcmg.device.service.vo.warehouse.InventoryCheckVO;
import xcmg.device.service.vo.warehouse.InventoryMaterialsSearchVO;
import xcmg.device.service.vo.warehouse.InventoryMaterialsVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by GYJ on 2018-03-08.
 */
@Slf4j
@Service
public class InventoryCheckService {
    @Autowired
    private InventoryCheckMapper inventoryCheckMapper;
    @Autowired
    private GenDocumentService genDocumentService;
    @Autowired
    private WarehouseInventoryMapper warehouseInventoryMapper;
    @Autowired
    private FastGenClient fastGenClient;
    @Autowired
    private InventoryCheckDetailMapper inventoryCheckDetailMapper;
    @Autowired
    private CommonService commonService;
    @Autowired
    private PriMaterialsMapper materialsMapper;

    @Autowired
    private MaterialWarehouseUserMapper materialWarehouseUserMapper;
    @Autowired
    private WarehouseAreaLocationDOMapper warehouseAreaLocationDOMapper;
    @Autowired
    private WarehouseDOMapper warehouseDOMapper;

    @Autowired
    private HttpServletResponse response;
    @Autowired
    private DepartmentWarehouseRelationService relationService;


    public PageCommonVO list(SearchCommonVO<InventoryCheckVO> condition) {
        PageCommonVO pageCommonVO = new PageCommonVO();
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        PageHelperPlus.orderBy("create_date desc");
        List<InventoryCheckVO> viewList = new ArrayList<>();
        List<InventoryCheckDO> doList = inventoryCheckMapper.list(condition.getFilters());
        for (InventoryCheckDO inventoryCheckDO : doList) {
            InventoryCheckVO inventoryCheckVO = new InventoryCheckVO();
            BeanUtils.copyProperties(inventoryCheckDO, inventoryCheckVO);

            viewList.add(inventoryCheckVO);
        }
        pageCommonVO.setPageInfo(new PageInfo(doList));
        pageCommonVO.setPageInfoList(viewList);
        return pageCommonVO;
    }


    public PageCommonVO warehosueInventoryList(SearchCommonVO<InventoryCheckVO> condition,String deptId) {
        PageCommonVO pageCommonVO = new PageCommonVO();
        WareHousePermission wareHousePermission = relationService.hasWarehouseInveryControPermisson(condition.getFilters().getOrgId(), deptId);
        if (!wareHousePermission.isHasPermission()) {
            pageCommonVO.setPageInfo(new PageInfo(Collections.EMPTY_LIST));
            pageCommonVO.setPageInfoList(Collections.EMPTY_LIST);
            return pageCommonVO;
        }
        List<String> permissionWarehouses = wareHousePermission.getPermissionWarehouses();
        condition.getFilters().setWarehousePermitList(permissionWarehouses);
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        PageHelperPlus.orderBy("create_date desc");
        List<InventoryCheckVO> viewList = new ArrayList<>();
        List<InventoryCheckDO> doList = inventoryCheckMapper.list(condition.getFilters());
        for (InventoryCheckDO inventoryCheckDO : doList) {
            InventoryCheckVO inventoryCheckVO = new InventoryCheckVO();
            BeanUtils.copyProperties(inventoryCheckDO, inventoryCheckVO);

            viewList.add(inventoryCheckVO);
        }
        pageCommonVO.setPageInfo(new PageInfo(doList));
        pageCommonVO.setPageInfoList(viewList);
        return pageCommonVO;
    }

    /**
     * @Des 按照仓库类型盘点
     * @Date 2020/11/4 9:28
     * @Author wangzhaoyu
     * @Param [inventoryCheckVO]
     * @Return xcmg.device.infra.ErrorCode
     */
    @Async
    @Transactional
    public void warehouseTypeCheckAdd(InventoryCheckVO inventoryCheckVO) throws Exception {
        log.info("根据仓库类型盘点参数为{}",inventoryCheckVO);
        List<WarehouseDO> warehouseDOS = warehouseDOMapper.selectWarehousesByWarehouseType(inventoryCheckVO.getOrgId(), inventoryCheckVO.getWarehouseType(), null);
        log.info("根据仓库类型盘点未指定仓库，根据类型查询到的仓库为{}",warehouseDOS);
        if(CollectionUtils.isEmpty(warehouseDOS)){
            log.info("根据仓库类型盘点没有需要盘点单的仓库信息，直接返回");
            return ;
        }
        List<String> warehouseCodes = warehouseDOS.stream().map(x -> x.getWarehouseCode()).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(warehouseCodes)){
            log.info("根据仓库类型盘点没有需要盘点单的仓库信息，直接返回");
            return ;
        }
        for(String warehouseCode : warehouseCodes){
            warehouseTypeCheckAdd(warehouseCode,inventoryCheckVO);
        }
    }

    private void warehouseTypeCheckAdd(String wareHouseCode,InventoryCheckVO inventoryCheckVO) throws Exception {
        InventoryMaterialsSearchVO inventoryMaterialsSearchVO=new InventoryMaterialsSearchVO();
        inventoryMaterialsSearchVO.setOrgId(inventoryCheckVO.getOrgId());
        inventoryMaterialsSearchVO.setWarehouseCode(wareHouseCode);
        List<InventoryMaterialsVO> results=warehouseInventoryMapper.invenitoryMaterialsListByBarcode(inventoryMaterialsSearchVO);//根据零件条码查询零件
        //先判断是否有判断零件，如果没有直接返回
        if(CollectionUtils.isEmpty(results)){
            log.info("根据仓库类型生成盘点盘，仓库【{}】没有盘点零件，直接返回",wareHouseCode);
            return;
        }
        InventoryCheckDO inventoryCheckDO=new InventoryCheckDO();
        BeanUtils.copyProperties(inventoryCheckVO,inventoryCheckDO);
        inventoryCheckDO.setWarehouseCode(wareHouseCode);
        String code=genDocumentService.getDocumentNo(BillDesc.InventoryCheck);
        inventoryCheckDO.setCheckOrderNo(code);
        ActionResult<String> checkId=fastGenClient.textGuid();
        if(checkId==null||checkId.getCode()!=0){
            throw new Exception("生成盘点单获取生成主键异常");
        }
        //内部主键
        inventoryCheckDO.setId(checkId.getValue());
        if(0>inventoryCheckMapper.insert(inventoryCheckDO)){
            log.info("保存盘点单主表数据异常,保存数据未{}",inventoryCheckDO);
            throw new Exception("保存盘点单主表数据异常");
        };
        for (InventoryMaterialsVO entity:results) {
            InventoryCheckDetailDO inventoryCheckDetailDO=new InventoryCheckDetailDO();
            inventoryCheckDetailDO.setOrgId(inventoryCheckDO.getOrgId());
            inventoryCheckDetailDO.setCheckId(checkId.getValue());
            inventoryCheckDetailDO.setBarCode(entity.getBarCode());
            inventoryCheckDetailDO.setAreaCode(entity.getAreaCode());
            inventoryCheckDetailDO.setCheckOrderNo(code);
            inventoryCheckDetailDO.setLoactionCode(entity.getLocationCode());
            inventoryCheckDetailDO.setMaterialsNo(entity.getMaterialsNo());
            inventoryCheckDetailDO.setSysAmount(entity.getAmount());
            inventoryCheckDetailDO.setWarhouseCode(entity.getWarehouseCode());
            inventoryCheckDetailDO.setDepositaryId(entity.getUserId());
            inventoryCheckDetailDO.setDepositaryName(entity.getUserName());
            int ret =   inventoryCheckDetailMapper.insert(inventoryCheckDetailDO);
            if(ret<=0){
                log.info("添加盘点单步骤，添加盘点单零件信息异常，保存数据未{}",inventoryCheckDetailDO);
                throw new Exception("添加盘点单步骤，添加盘点单零件信息异常！");
            }
        }
    }

    @Transactional
    public ErrorCode add(InventoryCheckVO inventoryCheckVO) throws Exception
    {
            InventoryCheckDO inventoryCheckDO=new InventoryCheckDO();
            BeanUtils.copyProperties(inventoryCheckVO,inventoryCheckDO);
            String code=genDocumentService.getDocumentNo(BillDesc.InventoryCheck);
            inventoryCheckDO.setCheckOrderNo(code);
            ActionResult<String> checkId=fastGenClient.textGuid();
            if(checkId==null||checkId.getCode()!=0){
            return ErrorCode.Failure;
            }
            //内部主键
            inventoryCheckDO.setId(checkId.getValue());
            StringBuilder locationStr=new StringBuilder();
            if(inventoryCheckVO.getLocationCodes()!=null){
                for (String str:inventoryCheckVO.getLocationCodes()) {
                    locationStr.append("|").append(str);
                }
                inventoryCheckDO.setLocationCodesStr(locationStr.toString().substring(1,locationStr.length()));
            }
            if(0>inventoryCheckMapper.insert(inventoryCheckDO)){
                return ErrorCode.Failure;
            };

        if(!StringUtils.isEmpty(inventoryCheckVO.getMaterialsNos())){
            String[] str = inventoryCheckVO.getMaterialsNos().trim().replace("，", ",").split(",");
            inventoryCheckVO.setMaterialsList(Arrays.asList((str)));
        }
        InventoryMaterialsSearchVO inventoryMaterialsSearchVO=new InventoryMaterialsSearchVO();
        inventoryMaterialsSearchVO.setLocationCodes(inventoryCheckVO.getLocationCodes());
        inventoryMaterialsSearchVO.setMaterialsNos(inventoryCheckVO.getMaterialsNos());
        inventoryMaterialsSearchVO.setMaterialsList(inventoryCheckVO.getMaterialsList());
        inventoryMaterialsSearchVO.setOrgId(inventoryCheckVO.getOrgId());
        inventoryMaterialsSearchVO.setUserIds(inventoryCheckVO.getUserIds());
        inventoryMaterialsSearchVO.setWarehouseCode(inventoryCheckVO.getWarehouseCode());
        inventoryMaterialsSearchVO.setShlefNo(inventoryCheckVO.getShlefNo());
        List<InventoryMaterialsVO> results=warehouseInventoryMapper.invenitoryMaterialsListByBarcode(inventoryMaterialsSearchVO);//根据零件条码查询零件
        for (InventoryMaterialsVO entity:results) {
            InventoryCheckDetailDO inventoryCheckDetailDO=new InventoryCheckDetailDO();
            inventoryCheckDetailDO.setOrgId(inventoryCheckDO.getOrgId());
            inventoryCheckDetailDO.setCheckId(checkId.getValue());
            inventoryCheckDetailDO.setBarCode(entity.getBarCode());
            inventoryCheckDetailDO.setAreaCode(entity.getAreaCode());
            inventoryCheckDetailDO.setCheckOrderNo(code);
            inventoryCheckDetailDO.setLoactionCode(entity.getLocationCode());
            inventoryCheckDetailDO.setMaterialsNo(entity.getMaterialsNo());
            inventoryCheckDetailDO.setSysAmount(entity.getAmount());
            inventoryCheckDetailDO.setWarhouseCode(entity.getWarehouseCode());
            inventoryCheckDetailDO.setDepositaryId(entity.getUserId());
            inventoryCheckDetailDO.setDepositaryName(entity.getUserName());


          int ret =   inventoryCheckDetailMapper.insert(inventoryCheckDetailDO);
            if(ret<=0)
            {
                throw new Exception("添加盘点单步骤，添加盘点单零件信息异常！");
            }
        }
        return ErrorCode.Success;

    }

    public InventoryCheckVO item(String inventoryCheckId){
        InventoryCheckVO inventoryCheckVO=new InventoryCheckVO();
        InventoryCheckDO inventoryCheckDO=inventoryCheckMapper.item(inventoryCheckId);
        if(inventoryCheckDO==null){
            return null;
        }
        BeanUtils.copyProperties(inventoryCheckDO,inventoryCheckVO);
        String warehouseCode = inventoryCheckDO.getWarehouseCode();
        if(!StringUtils.isEmpty(warehouseCode)){
            WarehouseDO warehouseDO = warehouseDOMapper.itemByWarehouseCode(inventoryCheckDO.getOrgId(), warehouseCode);
            if(warehouseDO != null){
                inventoryCheckVO.setWarehouseDes(warehouseDO.getDescription());
            }
        }
        List<InventoryCheckDetailVO> voList=new ArrayList<>();
        List<InventoryCheckDetailDO> doList=inventoryCheckDetailMapper.list(inventoryCheckId);
        for (InventoryCheckDetailDO inventoryCheckDetailDO:doList) {
            InventoryCheckDetailVO inventoryCheckDetailVO=new InventoryCheckDetailVO();
            BeanUtils.copyProperties(inventoryCheckDetailDO,inventoryCheckDetailVO);
            voList.add(inventoryCheckDetailVO);
        }
        inventoryCheckVO.setInventoryCheckDetailVOS(voList);
        return  inventoryCheckVO;
    }


    public InventoryCheckPrintVO printItem(String inventoryCheckId){
        InventoryCheckPrintVO inventoryCheckPrintVO=new InventoryCheckPrintVO();
        InventoryCheckVO inventoryCheckVO=new InventoryCheckVO();
        InventoryCheckDO inventoryCheckDO=inventoryCheckMapper.item(inventoryCheckId);
        if(inventoryCheckDO==null){
            return null;
        }
        BeanUtils.copyProperties(inventoryCheckDO,inventoryCheckVO);
        List<InventoryCheckDetailVO> voList=new ArrayList<>();
        List<InventoryCheckDetailDO> doList=inventoryCheckDetailMapper.list(inventoryCheckId);
        for (InventoryCheckDetailDO inventoryCheckDetailDO:doList) {
            if(null==inventoryCheckDetailDO)
            {
                continue;
            }
            InventoryCheckDetailVO inventoryCheckDetailVO=new InventoryCheckDetailVO();
            BeanUtils.copyProperties(inventoryCheckDetailDO,inventoryCheckDetailVO);
            PriMaterialsDO priMaterialsDO = materialsMapper.itemByNo(inventoryCheckDetailDO.getMaterialsNo(),inventoryCheckDetailDO.getOrgId());
            if(null!=priMaterialsDO)
            {
                inventoryCheckDetailVO.setMaterialsDes(priMaterialsDO.getMaterialsDes());
            }
            String loactionCode = inventoryCheckDetailVO.getLoactionCode();
            if (StringUtils.isNotBlank(loactionCode)){
                inventoryCheckDetailVO.setLocationDesc(warehouseAreaLocationDOMapper.getLocationDesc(loactionCode));
            }
            voList.add(inventoryCheckDetailVO);
        }
        inventoryCheckPrintVO.setHeaderInfo(inventoryCheckVO);
        inventoryCheckPrintVO.setDetailList(voList);
        return  inventoryCheckPrintVO;
    }
    /**
     * 开始盘点
     * @param inventoryCheckId
     * @param userId
     * @return
     */
    public ErrorCode runCheck(String inventoryCheckId,String userId){
        InventoryCheckDO inventoryCheckDO=inventoryCheckMapper.item(inventoryCheckId);
        if(inventoryCheckDO==null){
            return ErrorCode.ImportExcelNoData;
        }
        if(inventoryCheckDO.getStatus()!=0){
            return ErrorCode.CheckStatusIsFail;
        }
        if(inventoryCheckMapper.runCheck(inventoryCheckId,userId,commonService.getUserName(userId))==0){
            return ErrorCode.Failure;
      }
        return  ErrorCode.Success;
    }
    //结束盘点
    public ErrorCode stopCheck(String inventoryCheckId){
        InventoryCheckDO inventoryCheckDO=inventoryCheckMapper.item(inventoryCheckId);
        if(inventoryCheckDO==null){
            return ErrorCode.ImportExcelNoData;
        }
        if(inventoryCheckDO.getStatus()!=1){
            return ErrorCode.CheckStatusIsFail;
        }
        if(inventoryCheckMapper.stopCheck(inventoryCheckId)==0){
            return ErrorCode.Failure;
        }
        return  ErrorCode.Success;
    }

    /**
     * 获取盘点单盘点情况信息
     * @param checkDetailVO InventoryCheckDetailVO
     * @return InventoryCheckCountVO
     */
    public InventoryCheckCountVO itemCount(InventoryCheckDetailVO checkDetailVO)
    {
        InventoryCheckCountVO checkCountVO = new InventoryCheckCountVO();

        //查询未盘点数
        checkDetailVO.setState(0);
        Integer undoneCheck = inventoryCheckDetailMapper.itemCount(checkDetailVO);

        //查询已盘点数
        checkDetailVO.setState(1);
        Integer doneCheck = inventoryCheckDetailMapper.itemCount(checkDetailVO);

        //查询盘点异常数量
        checkDetailVO.setState(1);
        checkDetailVO.setResultType(1);
        Integer diffCheck = inventoryCheckDetailMapper.itemCount(checkDetailVO);

        checkCountVO.setCheckOrderNo(checkDetailVO.getCheckOrderNo());
        checkCountVO.setUndoneCheck(undoneCheck);
        checkCountVO.setDoneCheck(doneCheck);
        checkCountVO.setDiffCheck(diffCheck);
        return checkCountVO;
    }



    //盘点单详情列表
    public PageCommonVO itemList(SearchCommonVO<InventoryCheckDetailVO> condition) {
        PageCommonVO pageCommonVO = new PageCommonVO();
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        PageHelperPlus.orderBy("id desc");
        List<InventoryCheckDetailVO> viewList = new ArrayList<>();

        InventoryCheckDetailVO filter = condition.getFilters();

        if (null != filter.getState() && 2 == filter.getState()) {
            //查询已盘点、有差异的数据
            filter.setState(1);
            filter.setResultType(1);
        }

        List<InventoryCheckDetailDO> doList=inventoryCheckDetailMapper.itemList(condition.getFilters());
        for (InventoryCheckDetailDO inventoryCheckDetailDO:doList) {
            InventoryCheckDetailVO inventoryCheckDetailVO=new InventoryCheckDetailVO();
            BeanUtils.copyProperties(inventoryCheckDetailDO,inventoryCheckDetailVO);
            PriMaterialsDO priMaterialsDO=materialsMapper.itemByNo(inventoryCheckDetailDO.getMaterialsNo(),inventoryCheckDetailDO.getOrgId());
            if(priMaterialsDO!=null){
                inventoryCheckDetailVO.setMaterialsDes(priMaterialsDO.getMaterialsDes());
            }
            //inventoryCheckDetailVO.setMaterialsDes(materialsMapper.itemByNo(inventoryCheckDetailDO.getMaterialsNo(),inventoryCheckDetailDO.getOrgId()).getMaterialsDes());
            viewList.add(inventoryCheckDetailVO);
        }
        pageCommonVO.setPageInfo(new PageInfo(doList));
        pageCommonVO.setPageInfoList(viewList);
        return pageCommonVO;
    }

    //扫描条码查询信息
    public InventoryCheckDetailVO itemByBarCode(String barCode,String checkOrderNo){
        InventoryCheckDetailVO inventoryCheckDetailVO=new InventoryCheckDetailVO();
        InventoryCheckDetailDO inventoryCheckDetailDO=inventoryCheckDetailMapper.itemByBarCode(barCode,checkOrderNo);
        if(null==inventoryCheckDetailDO)
        {
            return  null;
        }
        BeanUtils.copyProperties(inventoryCheckDetailDO,inventoryCheckDetailVO);
        inventoryCheckDetailVO.setMaterialsDes(warehouseInventoryMapper.selectByBarCode(inventoryCheckDetailDO.getBarCode(),inventoryCheckDetailDO.getOrgId()).getMaterialName());

        return  inventoryCheckDetailVO;
    }
    //盘点确认
    public ErrorCode inventoryCheck(InventoryCheckDetailVO model){
        InventoryCheckDO inventoryCheckDO=inventoryCheckMapper.itemByCheckOrderNo(model.getCheckOrderNo());
        if(inventoryCheckDO==null||!"1".equals(inventoryCheckDO.getStatus().toString())){
            return ErrorCode.CheckStatusIsFail;
        }
        InventoryCheckDetailDO inventoryCheckDetailDO=inventoryCheckDetailMapper.itemByBarCode(model.getBarCode(),model.getCheckOrderNo());
        if(inventoryCheckDetailDO==null){
            return ErrorCode.ImportExcelNoData;
        }
        if(inventoryCheckDetailDO.getState()==1){
            return ErrorCode.CannotDuplicate;
        }
        model.setInventoryUserName(commonService.getUserName(model.getInventoryUserId()));
        switch (inventoryCheckDetailDO.getSysAmount().compareTo(model.getActAmount())){
            case -1://盘盈
                model.setResultType(2);
                break;
            case 0:
                model.setResultType(0);
                break;
            case 1://盘亏
                model.setResultType(1);
                break;
            default:
                break;
        }
        if(inventoryCheckDetailMapper.inventoryCheck(model)==0){
            return ErrorCode.Failure;
        };
        return ErrorCode.Success;
    }

    //强制完成
    @Transactional
    public ErrorCode enforce(InventoryCheckVO inventoryCheckVO,String userId){
        InventoryCheckDO inventoryCheckDO=inventoryCheckMapper.item(inventoryCheckVO.getId());
        if(inventoryCheckDO==null){
            return ErrorCode.ImportExcelNoData;
        }
        if(inventoryCheckDO.getStatus()!=1){
            return ErrorCode.CheckStatusIsFail;
        }
        InventoryCheckDetailVO inventoryCheckDetailVO=new InventoryCheckDetailVO();
        inventoryCheckDetailVO.setCheckId(inventoryCheckVO.getId());
        inventoryCheckDetailVO.setInventoryUserName(commonService.getUserName(userId));
        inventoryCheckDetailVO.setInventoryUserId(userId);
        inventoryCheckDetailMapper.enforce(inventoryCheckDetailVO);
        inventoryCheckMapper.enforce(inventoryCheckVO);
        return ErrorCode.Success;
    }

    public List<MaterialsRelationVO> selectUserByWarehouse(String warehouseCode,String orgId)throws Exception {
        if(warehouseCode==null){
            return  new ArrayList<>();
        }
        List<MaterialWarehouseUserDO> doList=materialWarehouseUserMapper.selectUserByWarehouse(orgId,warehouseCode);
        List<MaterialsRelationVO> voList=new ArrayList<>();
        for (MaterialWarehouseUserDO userDO:doList
             ) {
            if(userDO==null){
                continue;
            }
            MaterialsRelationVO materialsRelationVO=new MaterialsRelationVO();
            BeanUtils.copyProperties(userDO,materialsRelationVO);
            voList.add(materialsRelationVO);
        }
        return voList;
    }

    public PageCommonVO invenitoryMaterialsList(SearchCommonVO<InventoryMaterialsSearchVO> condition){
        PageCommonVO pageCommonVO=new PageCommonVO();
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        if(condition.getFilters()!=null&&!StringUtils.isEmpty(condition.getFilters().getMaterialsNos())){
            String[] str = condition.getFilters().getMaterialsNos().trim().replace("，", ",").split(",");
            condition.getFilters().setMaterialsList(Arrays.asList((str)));
        }
        if(condition.getFilters()!=null&&condition.getFilters().getUserIds()!=null&&condition.getFilters().getUserIds().isEmpty()){
            condition.getFilters().setUserIds(null);
        }
        List<InventoryMaterialsVO> results=warehouseInventoryMapper.invenitoryMaterialsList(condition.getFilters());
        pageCommonVO.setPageInfo(new PageInfo(results));
        pageCommonVO.setPageInfoList(results);
        return pageCommonVO;
    }

    public ErrorCode remove(List<String> ids){
        for (String id:ids) {
            InventoryCheckDO inventoryCheckDO=inventoryCheckMapper.item(id);
            if(inventoryCheckDO==null){
                return ErrorCode.ImportExcelNoData;
            }
            if(inventoryCheckDO.getStatus()!=0){
                return ErrorCode.CannotRemoveBeginBill;
            }
            inventoryCheckMapper.remove(id);
            inventoryCheckDetailMapper.remove(id);
        }
        return ErrorCode.Success;
    }

    public PageCommonVO listReport(SearchCommonVO<InventoryCheckVO> condition){
        PageCommonVO pageCommonVO=new PageCommonVO();
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        List<InventoryCheckReportDO> list = inventoryCheckMapper.selectInventoryCheckReport(condition.getFilters());
        pageCommonVO.setPageInfo(new PageInfo<>(list));
        pageCommonVO.setPageInfoList(list);
        return pageCommonVO;
    }

    public void export(List<InventoryCheckReportDO> list) throws Exception {
        invokeExport(list);
    }

    public void exportAll(InventoryCheckVO inventoryCheckVO) throws Exception {
        List<InventoryCheckReportDO> list = inventoryCheckMapper.selectInventoryCheckReport(inventoryCheckVO);
        invokeExport(list);
    }

    private void invokeExport(List<InventoryCheckReportDO> doList)throws Exception{
        String title = "盘点单报表导出";
        String[] rowsName = new String[]{"序号","盘点单号","盘点描述","单据状态","仓库",
                "计划盘点日期", "盘点条码", "零件编码", "零件名称","储位","原始数量",
                "盘点数量", "盘点状态", "盘点员", "盘点时间", "保管员"};
        List<Object[]>  dataList = new ArrayList<Object[]>();
        Object[] objs;
        for (int i = 0; i < doList.size(); i++) {
            InventoryCheckReportDO exportDO = doList.get(i);
            objs = new Object[rowsName.length];
            objs[0] = i+1;
            objs[1] = exportDO.getCheckOrderNo();
            objs[2] = exportDO.getCheckOrderDes();
            objs[3] = getStatus(exportDO.getStatus());
            objs[4] = exportDO.getWarehouseCode();
            objs[5] = exportDO.getPlannedDate()==null ? "" : DateUtil.formatDateTime(exportDO.getPlannedDate());
            objs[6] = exportDO.getBarCode();
            objs[7] = exportDO.getMaterialsNo();
            objs[8] = exportDO.getMaterialsDes();
            objs[9] = exportDO.getLocationCode();
            objs[10] = exportDO.getSysAmount();
            objs[11] = exportDO.getActAmount();
            objs[12] = getResultType(exportDO.getResultType());
            objs[13] = exportDO.getInventoryUserName();
            objs[14] = exportDO.getInventoryDate() == null ? "" : DateUtil.formatDateTime(exportDO.getInventoryDate());
            objs[15] = exportDO.getDepositaryName();
            dataList.add(objs);
        }
        ExportUtil exportUtil = new ExportUtil(title, rowsName, dataList);
        exportUtil.export(response);
    }

    private String getStatus(Short status){
        String statusStr = "";
        switch (status){
            case 0:
                statusStr = "未盘点";
                break;
            case 1:
                statusStr = "启动盘点";
                break;
            case 3:
                statusStr = "盘点结束";
                break;
        }
        return statusStr;
    }

    private String getResultType(Integer resultType){
        String resultTypeDes = "";
        if (resultType==null) {
            return resultTypeDes;
        }
        switch (resultType) {
            case 0:
                resultTypeDes = "正常";
                break;
            case 1:
                resultTypeDes = "盘亏";
                break;
            case 2:
                resultTypeDes = "盘盈";
                break;
            default:
                break;
        }
        return resultTypeDes;
    }


}
